package com.item3;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * @Author WXX
 * @Date 2020/7/17 20:07
 * @Version 1.0
 */
public class BNV_Fragment extends LazyFragment {

 /*   单例模式:
    FragmentStateAdapter内部自己会管理已实例化的fragment对象。
    也就是FragmentStateAdapter有一套机制实现什么时候销毁、什么时候创建
    */
    public static BNV_Fragment newInstance(String label) {
        Bundle args = new Bundle();
        args.putString("label", label);
        BNV_Fragment fragment = new BNV_Fragment();
        fragment.setArguments(args);
        Log.e(label+"地址：", ""+fragment );
        return fragment;
    }


    //绑定视图
    //调用一次
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState){
        Log.e("实现类", "onCreateView: " );
        return inflater.inflate(R.layout.fragment_bnv,container,false);
    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    //开始就设置文本
    @Override
    public void onStart() {
        Log.e("实现类", "onStart: " );
        super.onStart();
        String label = getArguments().getString("label");
        TextView text = getView().findViewById(R.id.tv_content);
        text.setText(label);

    }

    //ViewPage2的Fragment的最大生命周期为onStart
    @Override
    public void onResume() {
        final TextView text = getView().findViewById(R.id.tv_content);
        if(isFirstLoad){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        Thread.sleep(3000);//休眠3秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            text.setVisibility(View.VISIBLE);
                        }
                    });
                }
            }.start();
        }
        super.onResume();

    }

}

